//
//  LoginController.m
//  FavClient
//
//  Created by MagicStudio on 12-4-17.
//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//

#import "LoginController.h"
#import "MainController.h"
#import "FavHelper.h"



@implementation LoginController

@synthesize username ;
@synthesize password ;
@synthesize loginAlertDialog ;
@synthesize inputFrame ;
@synthesize logo ;
@synthesize mTabBarController ;
@synthesize mVersion ;

#pragma Mark-

-(void) closeKeybord{
    [self.username resignFirstResponder] ;
    [self.password resignFirstResponder] ;
}

-(void) startLogin:(id) params{
    NSString *u = [params objectAtIndex:0];
    NSString *p = [params objectAtIndex:1] ;
    [NSThread sleepForTimeInterval:3] ;
    if([u isEqualToString:@"admin"] && [p isEqualToString:@"admin"]){
        [self performSelectorOnMainThread:@selector(endLogin:) withObject:@"YES" waitUntilDone:YES] ;
    }else{
        [self performSelectorOnMainThread:@selector(endLogin:) withObject:@"NO" waitUntilDone:YES] ;
    }
}
-(void) endLogin : (NSString *) isSuccess {
    if([isSuccess isEqualToString:@"YES"]){
        [self.loginAlertDialog  dismissWithClickedButtonIndex:0 animated:YES] ;
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MainController" owner:self options:nil] ;
        for(id obj in nib){
            if([obj isKindOfClass:[MainController class]]){
                [self.view setFrame:CGRectMake(0, 0, 320, 480)] ;
                self.mTabBarController = (MainController *)obj ;
                
            }
        }
        CATransition *anim = [UIViewAnimationHelper getCATAnimation:1.0f type:Type_Cube] ;
        [ViewChangeHelper changeControllerForSelf:self.mTabBarController disAppearController:self catTrasition:anim] ;
    }else{
        [self.loginAlertDialog  dismissWithClickedButtonIndex:0 animated:YES] ;
        UIAlertView *failedAlertView = [[UIAlertView alloc] initWithTitle:@"对不起,登录失败" message:@"你的用户名或者密码不正确,请检查后再次登录" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil, nil] ;
        [failedAlertView show] ;
        [failedAlertView release] ;
        
    }
    //NSLog(@"%@",[NSString stringWithFormat:@"%@",isSuccess]) ;
}
-(IBAction) closeKeybordEvent:(id)sender{
    [self closeKeybord] ;
}
-(IBAction) loginEvent:(id)sender{
    NSString *u = self.username.text ;
    NSString *p = self.password.text ;
    if(self.loginAlertDialog == nil){
        self.loginAlertDialog = [UIAlertViewHelper showProgressDialog:@"正在登录,请稍等..." delegate:self alertSize:CGSizeMake(180, 180)] ;

    }else{
        [self.loginAlertDialog show] ;
    }
    NSArray *params = [NSArray arrayWithObjects:u,p, nil] ;
    [NSThread detachNewThreadSelector:@selector(startLogin:) toTarget:self withObject:params] ;
     
    
}

#pragma Mark-

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [username release] ;
    [password release] ;
    [inputFrame release] ;
    [logo release] ;
    [loginAlertDialog release] ;
    [mTabBarController release] ;
    [mVersion release] ;
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    
    [UIViewAnimationHelper doCATAnimationDefault:self.mVersion duration:1.0f type:kCATransitionPush subType:kCATransitionFromTop] ;
     [UIViewAnimationHelper doCATAnimationDefault:self.logo duration:1.0f type:kCATransitionPush subType:kCATransitionFromBottom] ;
    [self.username addTarget:self action:@selector(closeKeybord) forControlEvents:UIControlEventEditingDidEndOnExit] ;
    [self.password addTarget:self action:@selector(closeKeybord) forControlEvents:UIControlEventEditingDidEndOnExit] ;
    self.username.delegate = self ;
    [self.password setDelegate:self] ;
}

- (void)viewDidUnload
{

    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma Mark- UIAlertViewDelegate Method
- (void)willPresentAlertView:(UIAlertView *)alertView{
    alertView.frame = CGRectMake(70, 150, 180, 180) ;
}

#pragma Mark- UITextFieldDelegate Method
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    [UIViewAnimationHelper doCATAnimation:textField duration:1.0f type:Type_RippleEffect] ;
    return YES ;
}

@end
